package com.retch.wenchengmao.recodeauthutils.asyn;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.retch.wenchengmao.recodeauthutils.MainActivity;
import com.retch.wenchengmao.recodeauthutils.R;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * author:wenchengmao
 * time:  2017/3/28.
 * email: wenchengmao@yeah.net
 */
//asyn 网络请求demo
public class AsynActivity extends AppCompatActivity {
    private TextView data;
    public String url="login.php";
    private EditText name,password;
    private Button login;
    RequestParams params=new RequestParams();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asyn);
        initView();
    }

    public void initView()
    {
        data=(TextView)findViewById(R.id.data);
        name=(EditText)findViewById(R.id.name);
        password=(EditText)findViewById(R.id.password);
        login=(Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params.put("email",name.getText().toString());
                params.put("password",password.getText().toString());
                Log.d("email",name.getText().toString());
                NetworkUtil.post(url, params, new NetworkUtil.NetworkProcess() {
                    @Override
                    public void success(JSONObject jsonObject) {
                        Log.d("jsonObject",jsonObject.toString());
                        try{
                            String name=jsonObject.get("email").toString();
                            Log.d("name",name);
                            data.setText(jsonObject.toString()+"");
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void success() {
                        //登录成功的处理
                        Log.d("登录成功","登录成功!");
                        Toast.makeText(AsynActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void netError() {
                        //网络错误处理
                    }

                    @Override
                    public void infoError() {
                        //信息错误的处理
                    }

                    @Override
                    public void empty() {
                        Log.d("登录失败","无该用户");
                    }
                });
            }
        });
    }
}
